package com.sptpc.demo.controller;

import com.sptpc.demo.controller.requests.AddOrderRequest;
import com.sptpc.demo.model.Order;
import com.sptpc.demo.service.IOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private IOrderService orderService;

    @PostMapping("/")
    public Order addNewOrder(@RequestBody AddOrderRequest request) {
        log.info("订单信息:{}", request);
        Order order = new Order();
        BeanUtils.copyProperties(request, order);
        orderService.addOrder(order, request.getItems());

        return order;
    }
}

